package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.entity.ChatEntity;
import com.entity.MgcConfig;
import com.entity.view.ChatView;
import com.service.MgcConfigService;
import com.utils.MPUtil;
import com.utils.MgcUtils;
import com.utils.PageUtils;
import com.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 *
 */

@RestController
@RequestMapping("mgc")
public class MgcController {

    @Autowired
    private MgcConfigService mgcConfigService;

    @Autowired
    private MgcUtils mgcUtils;
    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, MgcConfig mgcConfig, HttpServletRequest request){

        EntityWrapper<MgcConfig> ew = new EntityWrapper<MgcConfig>();
        PageUtils page = mgcConfigService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, mgcConfig), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,MgcConfig mgcConfig, HttpServletRequest request){
//        if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
//            chat.setUserid((Long)request.getSession().getAttribute("userId"));
//        }
        EntityWrapper<MgcConfig> ew = new EntityWrapper<MgcConfig>();
        PageUtils page = mgcConfigService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, mgcConfig), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( MgcConfig chat){
        EntityWrapper<MgcConfig> ew = new EntityWrapper<MgcConfig>();
        ew.allEq(MPUtil.allEQMapPre( chat, "chat"));
        return R.ok().put("data", mgcConfigService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(MgcConfig chat){
        EntityWrapper< MgcConfig> ew = new EntityWrapper<MgcConfig>();
        ew.allEq(MPUtil.allEQMapPre( chat, "chat"));
        MgcConfig mgcConfig =  mgcConfigService.selectView(ew);
        return R.ok("查询客服聊天表成功").put("data", mgcConfig);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        MgcConfig chat = mgcConfigService.selectById(id);
        return R.ok().put("data", chat);
    }

    /**
     * 前端详情
     */
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        MgcConfig chat = mgcConfigService.selectById(id);
        return R.ok().put("data", chat);
    }




    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MgcConfig mgcConfig, HttpServletRequest request){
        mgcConfig.setId((new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()));
        //ValidatorUtils.validateEntity(chat);
        if(StringUtils.isBlank(mgcConfig.getMgName())) {
            return R.error("请填写敏感词");
        }

        boolean insert = mgcConfigService.insert(mgcConfig);
        if (insert){
            mgcUtils.initSensitive();
        }
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody MgcConfig mgcConfig, HttpServletRequest request){
        mgcConfig.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(chat);
//        mgcConfig.setUserid((Long)request.getSession().getAttribute("userId"));

        mgcConfigService.insert(mgcConfig);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MgcConfig mgcConfig, HttpServletRequest request){
        //ValidatorUtils.validateEntity(chat);

        boolean b = mgcConfigService.updateById(mgcConfig);//全部更新
        if (b){
            mgcUtils.initSensitive();
        }
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        boolean b = mgcConfigService.deleteBatchIds(Arrays.asList(ids));
        if (b){
            mgcUtils.initSensitive();
        }

        return R.ok();
    }
}
